package fl.ass.service.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.dto.SysUsrDatAuthRDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ass.facade.api.IAssTrailCarHvApi;
import fl.ass.facade.condition.AssTrailCarHvQueryCondition;
import fl.ass.facade.dto.AssTrailCarHvDTO;
import fl.ass.facade.dto.MessageDTO;
import fl.ass.service.service.IAssTrailCarHvService;

/**
 * 拖车交接单API
 * @author zy
 *
 */
@Component
public class AssTrailCarHvApi implements IAssTrailCarHvApi {
	
	private static final Logger logger = LoggerFactory.getLogger(AssTrailCarHvApi.class);
	
	@Autowired
	private IAssTrailCarHvService assTrailCarHvService;
	
	/**
	 * 权限服务层BEAN
	 */
	@Autowired
	private IPriorityApiService priorityApiService;

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssTrailCarHvApi#saveAssTrailCarHv(fl.ass.facade.dto.AssTrailCarHvDTO)
	 */
	@Override
	public MessageDTO<String> saveAssTrailCarHv(AssTrailCarHvDTO dto) {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			Long id = assTrailCarHvService.add(dto);
			response.setId(id);
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssTrailCarHvApi#updateAssTrailCarHv(fl.ass.facade.dto.AssTrailCarHvDTO)
	 */
	@Override
	public MessageDTO<String> updateAssTrailCarHv(AssTrailCarHvDTO dto) {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			boolean flag = assTrailCarHvService.modify(dto);
			response.setSuccess(flag);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssTrailCarHvApi#findAssTrailCarHvById(java.lang.Long)
	 */
	@Override
	public MessageDTO<AssTrailCarHvDTO> findAssTrailCarHvById(Long id) {
		MessageDTO<AssTrailCarHvDTO> response = new MessageDTO<AssTrailCarHvDTO>();
		if(null == id){
			response.setMessage("parameter：id is can not be null");
		}
		try {
			AssTrailCarHvDTO dto = assTrailCarHvService.selectById(id);
			if(null != dto){
				response.setData(dto);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssTrailCarHvApi#findAssTrailCarHvListByPage(cmm.mid.core.framework.page.Pager)
	 */
	@Override
	public Pager findAssTrailCarHvListByPage(Pager page) {
		AssTrailCarHvQueryCondition condition = (AssTrailCarHvQueryCondition)page.getCondition();
		if(null != condition){
			//数据权限控制
			if(null != condition.getUsrId() && StringUtils.isNotBlank(condition.getSysCd())){
				try {
					//需要权限
					List<SysUsrDatAuthRDTO> usrDatAuthRList = this.priorityApiService.searchSysUsrDatAuthRWithOrgCdList(condition.getUsrId(),null,condition.getSysCd());
			        if(CollectionUtils.isNotEmpty(usrDatAuthRList)){
			        	condition.setUsrDatAuthRList(usrDatAuthRList);
					}
				} catch (Exception e) {
                    BaseLogUtils.newLogger("获取用户数据权限失败").setThrowable(e).info();
				}
			}
		}
		return assTrailCarHvService.findAssTrailCarHvListByPage(page);
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssTrailCarHvApi#findPrjPrdInfoByDetailId(java.lang.Long)
	 */
	@Override
	public MessageDTO<AssTrailCarHvDTO> findPrjPrdInfoByDetailId(Long detailId) {
		MessageDTO<AssTrailCarHvDTO> response = new MessageDTO<AssTrailCarHvDTO>();
		if(null == detailId){
			response.setMessage("parameter：detailId is can not be null");
		}
		try {
			AssTrailCarHvDTO dto = assTrailCarHvService.findPrjPrdInfoByDetailId(detailId);
			if(null != dto){
				response.setData(dto);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}
	
	/**
     * 不分页查询拖车交接单
     * @param condition
     * @return
     */
	@Override
    public List<AssTrailCarHvDTO> searchHvList(AssTrailCarHvQueryCondition condition) throws BaseException {
		if(null == condition){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"查询对象不能为空!");
		}
		return this.assTrailCarHvService.searchList(condition);
	}
	
}
